package org.moonlight.jvm.natives.java.lang.object;

import org.moonlight.jvm.natives.AbstractNativeMethod;
import org.moonlight.jvm.rtda.exclusive.Frame;
import org.moonlight.jvm.rtda.share.Object;
import org.moonlight.jvm.rtda.share.heap.Class;

/**
 * 本地方法 clone()
 *
 * @author Moonlight
 * @createTime 2023/9/20 17:54
 **/
public class Clone extends AbstractNativeMethod {

    public Clone() {
        super(JAVA_LANG_OBJECT, "clone", "()Ljava/lang/Object;");
    }

    @Override
    public void nativeMethod(Frame frame) {
        Object that = frame.getLocalVars().getThis();

        Class cloneable = that.getClazz().getLoader().loadClass("java/lang/Cloneable");
        if (!that.getClazz().isImplements(cloneable)) {
            throw new RuntimeException("CloneNotSupportedException");
        }

        frame.getOperandStack().pushRef(that.myClone());
    }
}
